#[derive(Debug, Clone)]
pub struct gradle{
    isHead:bool,
    head:String,
    context:Vec<String>,
    nexTag:Vec<gradle>
}

impl gradle {
    pub fn new(the_head:String)->gradle{
        gradle{
            isHead :false ,
            head:the_head,
            context:Vec::new(),
            nexTag:Vec::new()
        }
        
    }

    pub fn new_head(the_head:String)->gradle{
        gradle{
            isHead :true ,
            head:the_head,
            context:Vec::new(),
            nexTag:Vec::new()
        }
    }

    // 设置头节点
    pub fn set_head(&mut self,the_head:String) {
        self.head = the_head;
    }

    pub fn get_head(&self)->String{
        return self.head.to_string();
    }


    //添加内容
    pub fn add_context(&mut self,the_context:String) {
         self.context.push(the_context);
    }
    
    pub fn next(&mut self,the_head:String)->Option<&mut gradle>{
        let mut index = 0;
        for i in self.nexTag.to_vec(){
            if i.head == the_head{
                return Some(&mut self.nexTag[index]);
            }
            index = index +1;
        }
        return None
    }



    //添加下一级头节点 
    pub fn add_tag(&mut self,the_grade:gradle) {
        self.nexTag.push(the_grade);
    }

    // 递归获得下一个节点
    pub fn to_string(&self,index:&usize)->String{
       
        let mut text_context:String = "".to_string();
        //如果是不是头节点就得加 head{}
        if !self.isHead{
            text_context.push_str(self.head.as_str());
            text_context.push_str("{ \n");
        }

        for i in self.context.clone(){
            text_context.push_str(&format!("{:width$}","",width = index ));
            text_context.push_str(i.as_str());
            text_context.push_str("\n");
           
        }
        //递归添加子节点的to_string 顺带把 }加上
        for i in self.nexTag.to_vec(){
             text_context.push_str(&format!("{:width$}","",width = index ));
            text_context.push_str(&i.to_string(&(index+index)));
            text_context.push_str("\n ");
            text_context.push_str(&format!("{:width$}","",width = index ));
            text_context.push_str("} \n")
           
        }
       

        return text_context;

    }

    // pub fn get_tag(&self)->&gradle{
    //     return self.nexTag[1];
    // }
}


pub fn parise_tag(context_list:Vec<&str>,mut gradle:gradle) ->gradle {
    //left 用于储存左大括号
    let mut left: Vec<i32> = Vec::new();
    let mut left_if:i32 = 0;
    let mut index = 0;
    let b = context_list.clone();

    for mut i in context_list{
        if i.find("{").is_some(){
            if i.find("if").is_some()||i.find("else").is_some() {
                left_if = left_if+1;
            }else{
                left.push(index);
            }
            
        }
        if i.find("}").is_some(){
            if left_if!=0{
                let mut b:String =i.trim().to_string();
                //当处于当前节点的时候 把内容添加到 gradle context 里面
                if left.len() ==0  {
                    if left_if == 0 {
                        b = b.replace("}", "");
                    }
                   
                    gradle.add_context(b);
                }
                left_if = left_if-1;
            }else{
                if left.len() ==1  {
                    let start = left[left.len()-1] as usize;
                    let a =   &b[start..(index+1) as usize] ;
                   
                   
                    //截去现有母节点
                    let  b = &a[1..a.len()-1 as usize];
                    //将b &[&str] 转化为 c vec<&str>
                    let mut c:Vec<&str> = Vec::new();
                    c.extend(b.iter().copied());
                    //这里创建gradle节点
                    let head = a[0].replace("{", "");
                     println!("create Tag{}",head);
                    let item_gradle:gradle=gradle::new(head.trim().to_string());
                
                    //递归再查询节点
                    let item_gradle =  parise_tag(c,item_gradle);
    
                    gradle.add_tag(item_gradle);
                  
                    
                }
                //如果不是当前节点的子节点 而是 子节点的子节点的话就跳过 
                left.remove(left.len()-1);
               
            }
           
        }else{
            let mut b:String =i.trim().to_string();
            //当处于当前节点的时候 把内容添加到 gradle context 里面
            if left.len() ==0  {
                if left_if == 0 {
                    b = b.replace("}", "");
                }
               
                gradle.add_context(b);
            }
        }
   

        index = index+1;
    }
    if  left.len() != 0{
        let the_gradle:gradle = gradle::new("gradle_error".to_string());
        return the_gradle
    }

    return gradle; 

}